package juc_11_pool;

import java.util.concurrent.*;

public class CustomThreadPoolDemo {

    public static void main(String[] args) {
        int maximumPoolSize = Runtime.getRuntime().availableProcessors();
        System.out.println("CPU 核心数量: " + maximumPoolSize);

        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                Runtime.getRuntime().availableProcessors(),
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());

        try {
            for (int i = 0; i < 9; i++) {
                executorService.execute(()->{
                    try {
                        System.out.println(Thread.currentThread().getName() + " ok");
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }

        System.out.println("main 1 ...");
    }
}
